package com.yxx.service.Impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.Album;
import com.yxx.entity.Photo;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.AlbumQuery;
import com.yxx.entityModel.vo.request.AlbumRequest;
import com.yxx.entityModel.vo.response.AlbumBackResponse;
import com.yxx.entityModel.vo.response.AlbumResponse;
import com.yxx.enums.FilePathEnum;
import com.yxx.mapper.AlbumMapper;
import com.yxx.mapper.PhotoMapper;
import com.yxx.service.AlbumService;
import com.yxx.service.BlogFileService;
import com.yxx.strategy.context.UploadStrategyContext;
import com.yxx.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author yuxiangxun
 * @date 2025/2/11
 * @apiNote
 */
@Slf4j
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {

    @Resource
    private AlbumMapper albumMapper;

    @Resource
    private BlogFileService blogFileService;

    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Resource
    private PhotoMapper photoMapper;

    @Override
    public PageResult<AlbumBackResponse> listAlbumBackVO(AlbumQuery albumQuery) {
        // 查询相册数量
        Long count = albumMapper.selectCount(new LambdaQueryWrapper<Album>()
                .like(StringUtils.hasText(albumQuery.getKeyword()), Album::getAlbumName, albumQuery.getKeyword())
            );
        // 如果相册数量为0，则new一个分页结果对象并返回
        if(count == 0){
            return new PageResult<>();
        }
        // 如果相册数量不为0，则分页查询后台相册列表信息并返回
        List<AlbumBackResponse> albumBackList = albumMapper.selectAlbumInfoListVO(albumQuery);
        return new PageResult<>(albumBackList,count);
    }

    /**
     * 上传相册封面
     * @param file
     * @return
     */
    @Override
    public String uploadAlbumCover(MultipartFile file) {
        // 根据上传策略获取文件的上传路径
        String url = uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.PHOTO.getPath());
        blogFileService.saveBlogFile(file,url,FilePathEnum.PHOTO.getFilePath());
        return url;
    }

    /**
     * 添加相册
     * @param albumRequest
     */
    @Override
    public void addAlbum(AlbumRequest albumRequest) {
        // 判断相册名是否存在
        Album existAlbum = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                .select(Album::getId).eq(Album::getAlbumName, albumRequest.getAlbumName()));
        // 断言，存在相册名，抛出异常
        Assert.isNull(existAlbum,albumRequest.getAlbumName()+"相册已存在");
        // 通过断言，证明相册不存在，转换对象，添加相册
        Album newAlbum = BeanCopyUtils.copyBean(albumRequest, Album.class);
        baseMapper.insert(newAlbum);
    }

    /**
     * 删除相册
     * @param albumId
     */
    @Override
    public void deleteAlbum(Integer albumId) {
        // 相册下有n张照片，先查照片数量，不存在照片直接删除，存在照片就断言，抛出异常
        Long count = photoMapper.selectCount(new LambdaQueryWrapper<Photo>().eq(Photo::getAlbumId, albumId));
        Assert.isFalse(count > 0 , "相册下存在照片");
        // 不存在照片，直接删除相册
        albumMapper.deleteById(albumId);
    }

    /**
     * 修改相册
     * @param albumRequest
     */
    @Override
    public void updateAlbum(AlbumRequest albumRequest) {
        // 为了避免修改后的相册名称与数据库中的相册名称重复，这里判断一下，如果修改后的相册名称与数据库中的相册名称重复（说明数据库中存在同名的相册），断言抛异常
        Album repeatAlbum = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                        .select(Album::getId,Album::getAlbumName)
                        .eq(Album::getAlbumName,albumRequest.getAlbumName()));
        // 断言，如果数据库中存在同名的相册对象且存在的同名相册对象的id与当前修改对象的id不相同，抛出异常
        Assert.isFalse(Objects.nonNull(repeatAlbum) && !repeatAlbum.getId().equals(albumRequest.getId()), albumRequest.getAlbumName()+"相册已存在,请重新修改");
        // 修改相册
        Album newAlbum = BeanCopyUtils.copyBean(albumRequest, Album.class);
        baseMapper.updateById(newAlbum);
    }

    /**
     * 编辑相册
     * @param albumId
     * @return
     */
    @Override
    public AlbumRequest editAlbum(Integer albumId) {
        // 回显数据信息
        return albumMapper.selectAlbumById(albumId);
    }

    /**
     * 查看相册列表
     * @return
     */
    @Override
    public List<AlbumResponse> listAlbumVO() {
        return albumMapper.selectAlbumVOList();
    }

}
